package cn.macswelle.campusecurity.raspberry1;

import cn.macswelle.campusecurity.raspberry1.service.CODetection;
import cn.macswelle.campusecurity.raspberry1.service.FireDetection;
import cn.macswelle.campusecurity.raspberry1.service.SmokeDetection;
import com.netflix.discovery.converters.Auto;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContext;

@Configuration
@SpringBootApplication(scanBasePackages = {"cn.macswelle.campusecurity.sdk",
  "cn.macswelle.campusecurity.sdk.controller",
  "cn.macswelle.campusecurity.sdk.service"})
@EnableCaching
@EnableDiscoveryClient
@ComponentScan({"cn.macswelle.campusecurity.sdk.controller",
  "cn.macswelle.campusecurity.sdk.service",
  "cn.macswelle.campusecurity.sdk",
  "cn.macswelle.campusecurity.raspberry1.service",
  "cn.macswelle.campusecurity.raspberry1.controller"})
@EnableFeignClients("cn.macswelle.campusecurity.feignapi.deviceManager")
public class Raspberry1Application {

  public static void main(String[] args) {
    SpringApplication.run(Raspberry1Application.class, args);
  }

  @Autowired
  private CODetection coDetection;
  @Autowired
  private FireDetection fireDetection;
  @Autowired
  private SmokeDetection smokeDetection;

  @Bean
  public Thread getCODThread() {
    Thread t1 = new Thread(coDetection);
    t1.start();
    return t1;
  }

  @Bean
  public Thread getFDThread() {
    Thread t2 = new Thread(fireDetection);
    t2.start();
    return t2;
  }

  @Bean
  public Thread getSDThread() {
    Thread t3 = new Thread(smokeDetection);
    t3.start();
    return t3;
  }

  @Bean
  public GpioController getGpio(){
    return GpioFactory.getInstance();
  }
}
